#include "qe_log.h"
#include "qe_assert.h"
#include "qe_memory.h"
#include "qe_dmaengine.h"



QELOG_DOMAIN("qe-dmaengine");



qe_dma_chan *qe_dma_request_chan(qe_dma_dev *dev, qe_int id)
{
    qe_ret ret;

    qe_dma_chan *chan = dev->ops->get_chan(dev, id);
    if (!chan) {
        qe_error("get chan[%d] failed", id);
        return QE_NULL;
    }

    if (chan->reference) {
        qe_warning("chan[%d] already in use", id);
        return QE_NULL;
    }

    if (!dev->ops->alloc_chan)
        return QE_NULL;

    if (dev->ops->alloc_chan) {
        ret = dev->ops->alloc_chan(chan);
        if (ret != qe_ok) {
            qe_error("alloc chan[%d] error:%d", ret);
        }
    }

    return chan;
}

qe_ret qe_dma_slave_config(qe_dma_chan *chan, qe_dma_config *config)
{
    qe_dma_dev *dev = chan->dev;

    if (dev->ops->config)
        return dev->ops->config(chan, config);

    return qe_err_notsupport;
}

void qe_dma_submit(qe_dma_tx_desc *desc)
{
	return desc->tx_submit(desc);
}

void qe_dma_async_issue_pending(qe_dma_chan *chan)
{
	chan->dev->ops->device_issue_pending(chan);
}